package com.wfs.myapp.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

@Slf4j
public class DateUtils {

    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String YMD = "yyyy-MM-dd";
    public static final String YMD_CN = "yyyy年MM月dd日";
    public static final String YMDHms = "yyyyMMddHHmmss";

    /**
     * LocalDateTime转String
     *
     * @param t
     * @param format
     * @return
     */
    public static <T> String dateToString(T t, String format) {
        if (t != null) {
            if (t instanceof LocalDateTime) {
                DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
                return df.format((LocalDateTime) t);
            } else if (t instanceof LocalDate) {
                DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
                return df.format((LocalDate) t);
            }
        }
        return null;
    }

    public static <T> String dateToCNString(T t, String format) {
        if (t instanceof LocalDateTime) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(format, Locale.SIMPLIFIED_CHINESE);
            return df.format((LocalDateTime) t);
        } else if (t instanceof LocalDate) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(format, Locale.SIMPLIFIED_CHINESE);
            return df.format((LocalDate) t);
        }
        return null;
    }

    public static LocalDate enStringToDate(String str, String format) {
        if (StringUtils.isBlank(str)) return null;
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format, Locale.ENGLISH);
        return LocalDate.parse(str, df);
    }

    /**
     * String转LocalDateTime
     *
     * @param str
     * @param format
     * @return
     */
    public static LocalDate stringToDate(String str, String format) {
        if (StringUtils.isBlank(str)) return null;
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDate.parse(str, df);
    }

    public static void main(String[] args) {
        System.out.println(LocalDate.parse("2025-01-15", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    }

    public static LocalDateTime stringToDateTime(String str, String pattern) {
        if (StringUtils.isBlank(str)) return null;
        DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(str, df);
    }


    public static LocalDateTime stringToLongLocalDateTime(String value) {

        return stringToLocalDateTime(value, YMDHMS);
    }
    public static LocalDateTime stringToLocalDateTime(String value, String format) {
        if (value == null)
            return null;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        LocalDateTime localDateTime = null;
        try {
            localDateTime = LocalDateTime.parse(value, formatter);
        } catch (Exception e) {
            log.error("DateUtility method : stringToLocalDateTime() on error.", e);
        }

        return localDateTime;
    }


}
